﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PluginCommon;
using WinAPI.User32;

namespace LogFormater
{
    public partial class MainUserControl : PluginFormBase
    {
        private readonly List<string> csAdded = new List<string>();
        private readonly List<string> csDeleted = new List<string>();
        private readonly List<string> csModifyed = new List<string>();
        private readonly List<string> otherAdded = new List<string>();
        private readonly List<string> otherDeleted = new List<string>();
        private readonly List<string> otherModifyed = new List<string>();
        private readonly List<string> sqlAdded = new List<string>();
        private readonly List<string> sqlDeleted = new List<string>();
        private readonly List<string> sqlModifyed = new List<string>();

        public MainUserControl()
        {
            int x = User32Dll.RegisterHotKey(Handle, 0x32, User32Dll.MOD_CONTROL, (uint)0x6E);
            InitializeComponent();
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x312)
            {
                if (m.WParam.ToInt32() == 0x32)
                {
                    if (!string.IsNullOrEmpty(Clipboard.GetText()))
                    {
                        string result = TransForm(Clipboard.GetText());
                        if (!string.IsNullOrEmpty(result))
                        {
                            Clipboard.SetText(result);
                        }
                    }
                    DoRequestCommand(this, new RequestCommandEventArgs() { Type = RequestCommandType.SetNotifyIconMessage, Message = "转换成功!" });
                }
            }
            base.WndProc(ref m);
        }

        private string TransForm(string source)
        {
            if (!string.IsNullOrEmpty(source))
            {
                ClearList();
                string[] logLines = source.Split(new string[] { "\r\n" },
                                                             StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < logLines.Length; i++)
                {
                    if (logLines[i].StartsWith("Modified :", StringComparison.CurrentCultureIgnoreCase))
                    {
                        string extension = GetExtension(logLines[i]);
                        if (".cs".Equals(extension, StringComparison.CurrentCultureIgnoreCase) || ".aspx".Equals(extension, StringComparison.CurrentCultureIgnoreCase))
                        {
                            csModifyed.Add(logLines[i].Substring(10));
                        }
                        else if (".sql".Equals(extension, StringComparison.CurrentCultureIgnoreCase))
                        {
                            sqlModifyed.Add(logLines[i].Substring(10));
                        }
                        else
                        {
                            otherModifyed.Add(logLines[i].Substring(10));
                        }
                    }
                    else if (logLines[i].StartsWith("Added :", StringComparison.CurrentCultureIgnoreCase))
                    {
                        string extension = GetExtension(logLines[i]);
                        if (".cs".Equals(extension, StringComparison.CurrentCultureIgnoreCase) || ".aspx".Equals(extension, StringComparison.CurrentCultureIgnoreCase))
                        {
                            csAdded.Add(logLines[i].Substring(7));
                        }
                        else if (".sql".Equals(extension, StringComparison.CurrentCultureIgnoreCase))
                        {
                            sqlAdded.Add(logLines[i].Substring(7));
                        }
                        else
                        {
                            otherAdded.Add(logLines[i].Substring(7));
                        }
                    }
                    else if (logLines[i].StartsWith("Deleted :", StringComparison.CurrentCultureIgnoreCase))
                    {
                        string extension = GetExtension(logLines[i]);
                        if (".cs".Equals(extension, StringComparison.CurrentCultureIgnoreCase) || ".aspx".Equals(extension, StringComparison.CurrentCultureIgnoreCase))
                        {
                            csDeleted.Add(logLines[i].Substring(9));
                        }
                        else if (".sql".Equals(extension, StringComparison.CurrentCultureIgnoreCase))
                        {
                            sqlDeleted.Add(logLines[i].Substring(9));
                        }
                        else
                        {
                            otherDeleted.Add(logLines[i].Substring(9));
                        }
                    }
                }

                SortingList();
                return GenerateReport();
            }
            return string.Empty;
        }

        private void SortingList()
        {
            sqlAdded.Sort();
            csAdded.Sort();
            otherAdded.Sort();

            sqlDeleted.Sort();
            csDeleted.Sort();
            otherDeleted.Sort();

            sqlModifyed.Sort();
            csModifyed.Sort();
            otherModifyed.Sort();
        }

        private void ClearList()
        {
            sqlAdded.Clear();
            csAdded.Clear();
            otherAdded.Clear();

            sqlDeleted.Clear();
            csDeleted.Clear();
            otherDeleted.Clear();

            sqlModifyed.Clear();
            csModifyed.Clear();
            otherModifyed.Clear();
        }

        private string GenerateReport()
        {
            StringBuilder stringBuilder = new StringBuilder();
            // added 
            if (sqlAdded.Count > 0)
            {
                stringBuilder.Append("Sql Added:\r\n");
                stringBuilder.AppendLine(ListToString(sqlAdded));
            }
            if (csAdded.Count > 0)
            {
                stringBuilder.Append("Aspx,Cs Added:\r\n");
                stringBuilder.AppendLine(ListToString(csAdded));
            }
            if (otherAdded.Count > 0)
            {
                stringBuilder.Append("Other Added:\r\n");
                stringBuilder.AppendLine(ListToString(otherAdded));
            }
            // modifed
            if (sqlModifyed.Count > 0)
            {
                stringBuilder.Append("Sql Modifed:\r\n");
                stringBuilder.AppendLine(ListToString(sqlModifyed));
            }
            if (csModifyed.Count > 0)
            {
                stringBuilder.Append("Aspx,Cs Modifed:\r\n");
                stringBuilder.AppendLine(ListToString(csModifyed));
            }
            if (otherModifyed.Count > 0)
            {
                stringBuilder.Append("Other Modifed:\r\n");
                stringBuilder.AppendLine(ListToString(otherModifyed));
            }
            //delted
            if (sqlDeleted.Count > 0)
            {
                stringBuilder.Append("Sql Deleted:\r\n");
                stringBuilder.AppendLine(ListToString(sqlDeleted));
            }
            if (csDeleted.Count > 0)
            {
                stringBuilder.Append("Aspx,Cs Deleted:\r\n");
                stringBuilder.AppendLine(ListToString(csDeleted));
            }
            if (otherDeleted.Count > 0)
            {
                stringBuilder.Append("Other Deleted:\r\n");
                stringBuilder.AppendLine(ListToString(otherDeleted));
            }
            stringBuilder.AppendLine("Cause Reason:\r\n\r\n");
            stringBuilder.AppendLine("How To Fix:\r\n\r\n");
            return stringBuilder.ToString();
        }

        public string ListToString(IList<string> list)
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                stringBuilder.AppendLine(list[i]);
            }
            return stringBuilder.ToString();
        }

        private static string GetExtension(string path)
        {
            int dotPositon = path.LastIndexOf('.');
            if (dotPositon > -1)
            {
                return path.Substring(dotPositon);
            }
            return string.Empty;
        }
    }
}
